#include <iostream>
#include <complex>
#include <fstream>
#define MAX_SAMPLE 5000000
using namespace std;

int find_csi(const complex<float> * in, const int total){
    
    complex<float> mark(5269, 5269);
    
    int i=0;
    
    for (i=0; i< total-2; i++){
        if (in[i] == mark and (in[i+3] == mark)){
            cout << "mark found!" << endl;
            return i;
        }
        continue;
    }

    return -1;
}

int read_bin(complex<float> * output, char * fname){
  FILE * fp = 0;
  long int fsize = 0;
  int read_len = 0;

  fp = fopen(fname, "rb");

  if (fp == 0){
    printf("cannot read binary file\n");
    return 0;
  }
  
  fseek (fp, 0, SEEK_END);
  fsize = ftell(fp); //get how many bytes the file has
  rewind(fp);
  
  long int sample_len = 2*sizeof(float);

  if (fsize/sample_len > MAX_SAMPLE)
    read_len = MAX_SAMPLE;
  else
    read_len = fsize/sample_len;
  
  //cout << fsize/sample_len << "  " << read_len << endl;

  read_len = fread(output, 2*sizeof(float), read_len, fp);
  return read_len;
}


int main(){
  
  complex<float> * y1 = new complex<float>[MAX_SAMPLE];
  read_bin(y1, "/home/symbol/data/before_agc_1");
  int mark = find_csi(y1, 50000);
  for (int i=0; i<50000; i++)
    cout << y1[i] << endl;

  cout << "the mark is " << mark << endl;
  return 0;
}

